package com.liuzu.esys.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.liuzu.esys.bo.IntoStorageBo;
import com.liuzu.esys.bo.StorageBo;
import com.liuzu.esys.common.LayuiPageVo;
import com.liuzu.esys.common.R;
import com.liuzu.esys.pojo.IntoStorage;
import com.liuzu.esys.pojo.Storage;
import com.liuzu.esys.service.StorageService;
import com.liuzu.esys.vo.StorageBarVo;
import com.liuzu.esys.vo.StorageNumberVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * 太上老君保佑：代码，绿绿绿绿绿！！！！！
 *
 * @Description :
 * @Author : 冯小冯
 * @Date : 2022-07-25 15:17
 */
@RestController
@RequestMapping("/storage")
public class StorageController {
    @Autowired
    private StorageService storageService;


    //分页显示信息和模糊查询
    @RequestMapping("storageList")
    public LayuiPageVo<Storage> storageList(@RequestParam  Map<String,Object> params){

        return storageService.storagePageData(params);
    }

    //添加库存信息
    @GetMapping("/storageadd")
    public R storageAdd(Storage storage){
        int i = storageService.storageAdd(storage);
        if(i==1){
            return R.ok();
        }
        return R.error();
    }

    //修改库存信息
    @RequestMapping("/storageupdate")
    public R storageUpdate(Storage storage){
        int i = storageService.storageUpdate(storage);
        if(i==1){
            return R.ok();
        }
        return R.error();
    }

    //删除库存信息
    @RequestMapping("/storagedelete")
    public R storageDelete(int id){
       int i = storageService.storageDelete(id);
       if(i==1){
           return R.ok();
       }
       return R.error();
    }

    //批量删除
    @RequestMapping("/deleteStoragesByIds")
    public R deleteStoragesByIds(String ids){
        int result = storageService.deleteStoragesByIds(ids);
        if (result>0){
            return R.ok();

        }else {
            return R.error();
        }
    }


    //商品和库存统计图
    @RequestMapping("/echartBar")
    public R echartBar(){

        List<StorageBarVo> list = storageService.nameBarData();

        return R.ok().data("nameBar",list);
    }


    //商品出库入库量比例图
    @RequestMapping("/numberBar")
    public R numeberBar(){

        List<StorageNumberVo> list = storageService.nameNumberBarData();

        return R.ok().data("namesBar",list);
    }


    //导出列表数据     把满足条件的数据 查询出来  写到excel 中  ，把excel 写到浏览器
    @RequestMapping("/export")
    public void export(Storage storage, HttpServletResponse response) throws IOException {
        //下载excel 的 响应数据格式
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 下载的文件名 是中文  要进行转码  要不然会出现乱码
        String filename = URLEncoder.encode("仓库库存信息表", "utf-8");

        // 告诉浏览器  下载文件
        response.setHeader("Content-Disposition","attachment;filename="+filename+".xlsx");

        //得到指向浏览器的输出流  向浏览器写输出
        ServletOutputStream outputStream = response.getOutputStream();

        ExcelWriterBuilder write = EasyExcel.write(outputStream, StorageBo.class);

        ExcelWriterSheetBuilder sheet = write.sheet();
        //准备满足条件的集合数据
        List<StorageBo> StorageExcelData =   storageService.StorageExcelData(storage);
        // 把这个集合数据 写到 输出流
        sheet.doWrite(StorageExcelData);
    }

}
